const req = require('../../utils/apifm-wxapi')
Page({
  data: {
    active: 122991,
    currentTab: 0,
    leftList: [],
    goodsId: 122991,
    categoryId: {},
    rightList: [],
    shops:[]
  },
  onLoad: function (options) {
    req.goodsCategory().then((res) => {
      if(!options.id){
        this.setData({
          leftList: res.data,
          goodsId: res.data[0].id,
          active:res.data[0].id
        })
      }else{
        this.setData({
        leftList: res.data,
        goodsId: options.id,
        active:options.id
      })
      }
      
      console.log(this.data.leftList)
      const obj = { categoryId: this.data.goodsId }
      console.log(obj)
      req.goods(obj).then((res) => {
        if (res.code == 0) {
          this.setData({
            rightList:res.data
          })
        }else{
          return
        }
        console.log(this.data.rightList)
      })
    })
  
    

  },
  // 选项卡  主要是通过判断e.target.id的值 设置对应的id显示
  switchNav: function (e) {
    // var page = this;
    var id = e.currentTarget.dataset.goodid;
    var goodsId = e.currentTarget.dataset.goodid
    var categoryId = { categoryId: goodsId }
    
    req.goods(categoryId).then((res) => {
      if (res.code == 0) {
        this.setData({
          rightList:res.data
        })
      }else{
        this.setData({
          rightList:[]
        })
      }
      console.log(this.data.rightList)
    })
    this.setData({
      active: id,
      goodsId,
      categoryId,
    });
  },
  search(e){
    console.log(e.detail.value)
    wx.navigateTo({
      url: `/pages/search/search?key=${e.detail.value}`,
    })
  },
  add(e){
    console.log(e.target.dataset.id)
    var Token = wx.getStorageSync('Token')
    var goodsId=e.target.dataset.id
    var number=1
    req.goodsCartadd(Token,goodsId,number).then((res)=>{
      console.log(res)
      wx.showToast({
        title: '添加成功',
      })
      if(res.code==0){
        wx.setTabBarBadge({
          index: 2,
          text: String(res.data.number),
        })
      }
      console.log(res.data.items)
      this.setData({
        shops:res.data.items
      })
      console.log(this.data.shops)
      wx.reLaunch({
        url: `/pages/shopping/shopping?shops=${this.data.shops}`,
      })
    })
  }
})
